<template>
  <div class="common-table">
    <el-table
     ref="refsTable"
      v-loading="loading"
      :data="tableData"
      border
      fit
      highlight-current-row
      style="width: 100%"
      v-bind="$attrs"
      v-on="$listeners"
    >
    <el-table-column label="序号" prop="id" align="center" width="50">
          <template slot-scope="scope">
            <span>{{ (scope.$index) + (currentPage-1)*innerPageSize }}</span>
          </template>
        </el-table-column>
      <el-table-column
        v-for="(column, index) in columns"
        :key="column.prop || index"
        :prop="column.prop"
        :label="column.label"
        :width="column.width"
        :align="column.align || 'center'"
      >
        <template slot-scope="scope">
          <slot :name="column.prop" :row="scope.row" :$index="scope.$index">
            {{ scope.row[column.prop] }}
          </slot>
        </template>
      </el-table-column>
      <slot name="append"></slot>
    </el-table>
    
    <div class="pagination-container" v-if="showPagination">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </div>
  </div>
</template>

<script>
export default {
  name: 'CommonTable',
  props: {
    columns: {
      type: Array,
      default: () => []
    },
    fetchData: {
      type: Function,
      required: true
    },
    queryData: {  // 添加查询参数
      type: Object,
      default: () => ({})
    },
    immediate: {  // 是否立即请求数据
      type: Boolean,
      default: true
    },
    pageSize: {  // 分页大小
      type: Number,
      default: 10
    },
    showPagination: {
      type: Boolean,
      default: true
    }
  },
  data() {
    return {
      currentPage: 1,
      innerPageSize: this.pageSize,
      tableData: [],
      loading: false,
      total: 0
    }
  },
  created() {
    if (this.immediate) {
      this.getTableData()
    }
  },
  mounted () {
      this.$nextTick(() => {
        // 根据浏览器高度设置初始高度
        this.tableHeight = window.innerHeight - this.$refs.refsTable.$el.offsetTop - 150
        // 监听浏览器高度变化，修改表格高度
        window.onresize = () => {
          this.tableHeight = window.innerHeight - this.$refs.refsTable.$el.offsetTop - 150
        }
      })
  },
  methods: {
    async getTableData() {
      if (!this.fetchData) return
      
      this.loading = true
      try {
        const params = {
          ...this.queryData,
          // page: this.currentPage,
          // limit: this.innerPageSize
        }
        const res = await this.fetchData(params)
        console.log(res.data, '----')
        if (res.code === 0) {  // 添加状态判断
          this.tableData = res.data
          this.total = res.total || res.data.length
          this.$emit('success', res)  // 成功回调
        } else {
          this.$message.error(res.message || '获取数据失败')
          this.$emit('error', res)  // 失败回调
        }
      } catch (error) {
        console.error(error)
        this.$message.error('系统错误')
        this.$emit('error', error)
      } finally {
        this.loading = false
        this.$emit('complete')  // 完成回调
      }
    },
    // 刷新方法
    refresh() {
      return this.getTableData()
    },
    // 重置方法
    reset() {
      this.currentPage = 1
      this.innerPageSize = this.pageSize
      return this.getTableData()
    },
    handleSizeChange(val) {
      this.innerPageSize = val
      this.getTableData()
    },
    handleCurrentChange(val) {
      this.currentPage = val
      this.getTableData()
    }
  },
  watch: {
    queryData: {
      handler() {
        this.currentPage = 1
        this.getTableData()
      },
      deep: true
    }
  }
}
</script>

<style lang="scss" scoped>
@import '../style.scss';

// .common-table {
//   .pagination-container {
//     padding: 20px 0;
//     text-align: center;
//   }
// }
</style>